
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="java.util.UUID" %>
<%@ page import="java.io.File" %>
<%--
  Created by IntelliJ IDEA.
  User: zhaoyang
  Date: 2020/8/4
  Time: 9:44 上午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>增加学生</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");

//    String sname=request.getParameter("stuName");
//    String sex=request.getParameter("stuSex");
//    String telephone=request.getParameter("stuPhone");
//    String qq=request.getParameter("stuQQ");
//    String wechat=request.getParameter("stuWechat");
//    int gradeid=new Integer(request.getParameter("stuGrade"));
//    String enterdate=request.getParameter("enterTime");
//    String fromcity=request.getParameter("fromCity");
//    String birthday=request.getParameter("stuBirth");
//    String address=request.getParameter("stuAddr");
//
//
//    Student student=new Student(sname,sex,telephone,qq,wechat,gradeid,enterdate,fromcity,birthday,address);
//    int result = StudentService.STUDENT_SERVICE.add(student);


    //文件上传
    FileItemFactory item=new DiskFileItemFactory();
    ServletFileUpload upload=new ServletFileUpload(item);
    upload.setHeaderEncoding("utf-8");
    List<FileItem> list=upload.parseRequest(request);
   for(FileItem fi:list){
        if(fi.isFormField()){
            //普通的表单元素
            out.print("<h2>"+fi.getFieldName()+":"+fi.getString("utf-8")+"</h2>");
        }else{
            //文件
            out.print("get file>>>>>>>>>>>>.");
            if (fi.getSize()==0){
                out.print("<h2>没有文件！</h2>");
                continue;
            }
            out.print("<h2>这是一个文件，文件名："+fi.getName()+"，元素名："+fi.getFieldName()+"</h2>");
            String path=application.getRealPath("images");
            //文件要换一个名字,保证后缀是相同
            String fName =fi.getName();
            String houZhui=fName.substring(fName.lastIndexOf("."));
            String fileName= UUID.randomUUID()+houZhui;
            File f=new File(path,fileName);
            out.print(f.getAbsolutePath());
            fi.write(f);



        }


    }



%>

<script>
// if (confirm("信息增加成功！")) {
// location.replace("s_studentAdd.jsp");
// } else {
// location.replace("../index.jsp");
// }



<%--    if (<%=result%>==1) {--%>
<%--        alter("信息增加成功！")--%>
<%--        location.replace("s_studentAdd.jsp");--%>
<%--    } else {--%>
<%--        alter("信息增加失败...")--%>
<%--        history.back(-1);--%>
<%--    }--%>
</script>

</body>
</html>
